<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(chartData = []) {
      const dates = chartData.map(item => item.date);
      const seriesData = {
        orderCount: chartData.map(item => item.orderCount || 0),
        orderCheckCount: chartData.map(item => item.orderCheckCount || 0),
        expectPriceCount: chartData.map(item => item.expectPriceCount || 0),
        outboundOrderCount: chartData.map(item => item.outboundOrderCount || 0)
      };

      this.chart.setOption({
        title: {
          // text: '近30天数据趋势'
          text: ''
        },
        tooltip: {
          trigger: 'axis',
          formatter: (params) => {
            return `日期：${params[0].axisValue}<br/>` +
              params.map(p => `${p.marker} ${p.seriesName}：${p.value}`).join('<br/>');
          }
        },
        xAxis: {
          type: 'category',
          data: dates,
          axisTick: { show: false },
          boundaryGap: false
        },
        yAxis: {
          type: 'value',
          axisTick: { show: false }
        },
        grid: {
          left: 10,
          right: 18,
          bottom: 20,
          top: 40,
          containLabel: true
        },
        legend: {
          data: ['回收订单', '检测订单', '估价订单', '出库订单']
        },
        series: [
          {
            name: '回收订单',
            type: 'line',
            smooth: true,
            data: seriesData.orderCount,
            itemStyle: { color: '#FF005A' },
            lineStyle: { color: '#FF005A', width: 2 },
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: '检测订单',
            type: 'line',
            smooth: true,
            data: seriesData.orderCheckCount,
            itemStyle: { color: '#3888fa' },
            lineStyle: { color: '#3888fa', width: 2 },
            areaStyle: { color: '#f3f8ff' },
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '估价订单',
            type: 'line',
            smooth: true,
            data: seriesData.expectPriceCount,
            itemStyle: { color: '#FFAA00' },
            lineStyle: { color: '#FFAA00', width: 2 },
            animationDuration: 2800,
            animationEasing: 'linear'
          },
          {
            name: '出库订单',
            type: 'line',
            smooth: true,
            data: seriesData.outboundOrderCount,
            itemStyle: { color: '#00CC99' },
            lineStyle: { color: '#00CC99', width: 2 },
            animationDuration: 2800,
            animationEasing: 'bounceOut'
          }
        ]
      });
    }

  }
}
</script>
